﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Ink;

namespace InkCanvasControls.Palettes
{
    /// <summary>
    /// Interaction logic for PenPalette.xaml
    /// </summary>
    public partial class PenPalette : UserControl, INotifyPropertyChanged
    {
        #region Constructor
        public PenPalette()
        {
            InitializeComponent();

            BlackColour.IsChecked = true;
            MediumWidth.IsChecked = true;
            SquareTip.IsChecked = true;
        }
        #endregion

        #region Handle style changes
        public Brush SelectedBrush { get; set; }
        private void ColourButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn != null)
            {
                Line line = btn.Content as Line;
                if (line != null)
                {
                    SelectedBrush = line.Stroke;
                    OnColourChanged((Color)SelectedBrush.GetValue(SolidColorBrush.ColorProperty));
                    NotifyPropertyChanged("SelectedBrush");
                }
            }
        }

        public double SelectedThickness { get; set; }
        private void WidthButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn != null)
            {
                Line line = btn.Content as Line;
                if (line != null)
                {
                    SelectedThickness = line.StrokeThickness;
                    OnThicknessChanged(SelectedThickness);
                    NotifyPropertyChanged("SelectedThickness");
                }
            }
        }

        public StylusTip SelectedTip { get; set; }
        private void TipButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn != null)
            {
                Shape shape = sender as Shape;
                if (shape != null)
                {
                    if (shape is Rectangle)
                    {
                        SelectedTip = StylusTip.Rectangle;
                    }
                    else
                    {
                        SelectedTip = StylusTip.Ellipse;
                    }
                    OnTipChanged(SelectedTip);
                    NotifyPropertyChanged("SelectedTip");
                }
            }
        }
        #endregion

        #region Attribute changed event support
        public event ColourChangedEventHandler ColourChanged;
        public delegate void ColourChangedEventHandler(Color colour);
        public void OnColourChanged(Color colour)
        {
            if (ColourChanged != null)
            {
                ColourChanged(colour);
            }
        }

        public event ThicknessChangedEventHandler ThicknessChanged;
        public delegate void ThicknessChangedEventHandler(double thickness);
        public void OnThicknessChanged(double thickness)
        {
            if (ThicknessChanged != null)
            {
                ThicknessChanged(thickness);
            }
        }

        public event TipChangedEventHandler TipChanged;
        public delegate void TipChangedEventHandler(StylusTip tip);
        public void OnTipChanged(StylusTip tip)
        {
            if (TipChanged != null)
            {
                TipChanged(tip);
            }
        }
        #endregion

        #region Show current settings
        internal void SetColour(Color colour)
        {
            BlackColour.IsChecked = false;
            RedColour.IsChecked = false;
            BlueColour.IsChecked = false;

            if (colour == Colors.Black)
            {
                BlackColour.IsChecked = true;
            }
            if (colour == Colors.Red)
            {
                RedColour.IsChecked = true;
            }
            if (colour == Colors.Blue)
            {
                BlueColour.IsChecked = true;
            }
        }

        internal void SetThickness(double thickness)
        {
            ThinWidth.IsChecked = false;
            MediumWidth.IsChecked = false;
            ThickWidth.IsChecked = false;

            thickness = Math.Floor(thickness);
            if (thickness == 1.0)
            {
                ThinWidth.IsChecked = true;
            }
            if (thickness == 2.0)
            {
                MediumWidth.IsChecked = true;
            }
            if (thickness == 4.0)
            {
                ThickWidth.IsChecked = true;
            }
        }

        internal void SetTip(StylusTip stylusTip)
        {
            SquareTip.IsChecked = false;
            RoundTip.IsChecked = false;

            if (stylusTip == StylusTip.Rectangle)
            {
                SquareTip.IsChecked = true;
            }
            if (stylusTip == StylusTip.Ellipse)
            {
                RoundTip.IsChecked = true;
            }
        }
        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
